import 'package:development_assistant/service/docker_service.dart';
import 'package:get/get.dart';

import '../model/docker_info.dart';

class DockerController extends GetxController{

  var dockerID=0.obs;
  var dockerFrom="".obs;
  var dockerName="".obs;
  var dockerCid="".obs;
  var dockerTag="".obs;
  var dockerFile="".obs;
  var dockerList=RxList<DockerInfo>();
  
  DockerService dockerService=DockerService();

  Future getDockerList() async{
    List<Map<String,dynamic>> list=await dockerService.getDockerList();
    dockerList.assignAll(list.map((e) => DockerInfo.fromJson(e)).toList());
  }

  Future getDockerInfo(int? searchID) async{
    List<Map<String,dynamic>> docker=await dockerService.getDockerInfo(searchID);
    DockerInfo dockerInfo=DockerInfo.fromJson(docker[0]);
    dockerID.value=dockerInfo.dockerID!;
    dockerFrom.value=dockerInfo.dockerFrom;
    dockerName.value=dockerInfo.dockerName;
    dockerCid.value=dockerInfo.dockerCid;
    dockerTag.value=dockerInfo.dockerTag;
    dockerFile.value=dockerInfo.dockerFile;
  }

  Future addDocker() async{
    DockerInfo dockerInfo=DockerInfo(null, dockerFrom.value, dockerName.value, dockerCid.value, dockerTag.value, dockerFile.value);
    await dockerService.addDocker(dockerInfo);
    Get.snackbar("保存Docker镜像".tr, "${'成功保存Docker镜像'.tr} : ${dockerName.value}");
    Get.toNamed("/Home",arguments: {"selectIndex":0});
  }

  Future delDocker() async{
    await dockerService.delDocker(dockerID.value);
    Get.snackbar("删除Docker镜像".tr, "${'成功删除Docker镜像'.tr} : ${dockerName.value}");
    Get.toNamed("/Home",arguments: {"selectIndex":0});
  }

  Future updateDocker() async{
    DockerInfo dockerInfo= DockerInfo(dockerID.value, dockerFrom.value, dockerName.value, dockerCid.value, dockerTag.value, dockerFile.value);
    Get.snackbar("修改Docker镜像".tr, "${'成功修改Docker镜像'.tr} : ${dockerName.value}");
    Get.toNamed("/Home",arguments: {"selectIndex":0});
    await dockerService.updateDocker(dockerInfo);
  }


}